<div class="layui-card">

    <div class="layui-card-body">
        <div class="layui-form toolbar">
            搜索：<input id="menus-edit-search" class="layui-input search-input" type="text" placeholder="输入订单号"/>&emsp;
            <button id="menus-btn-search" class="layui-btn icon-btn"><i class="layui-icon">&#xe615;</i>搜索</button>
        </div>

        <!-- 数据表格 -->
        <table class="layui-table" id="order-table" lay-filter="menus-table"></table>
    </div>
</div>

<!-- 表格操作列 -->
<script type="text/html" id="order-state">
    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="edit">修改</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>
<!--状态操作-->
<script type="text/html" id="switchTpl">
    <!-- 这里的checked的状态只是演示 -->
    <input type="checkbox" name="enable" value="{{d.ordeid}}" lay-skin="switch" lay-text="退款|支付" lay-filter="order"
           {{d.enable=='1'?'checked':''}}>
</script>
<script>
    layui.use(['form', 'table', 'util', 'config', 'admin', 'formSelects', 'layer'], function () {
        var table = layui.table;
        var layer = layui.layer;
        var admin = layui.admin;
        var config = layui.config;
        var form = layui.form;
        // 渲染表格
        function order(){
            table.render({
                elem: '#order-table',
                url: config.base_server + 'api-administrators/order/findAll',
                where: {
                    access_token: config.getToken().access_token
                },
                parseData: function (res) {//数据格式解析的回调函数，用于将返回的任意数据格式解析成 table 组件规定的数据格式。
                    return {
                        "code": 0,
                        "msg": "",
                        "count": res.data.count,
                        "data": res.data.orderList
                    }
                },
                page: true,
                cols: [[
                    {type: 'numbers'},
                    {field: 'orcode', minWidth: 200, title: '订单号'},
                    {field: 'crdate', title: '创建时间'},
                    {field: 'remark', title: '订单备注'},
                    {field: 'enable', width: 95, templet: '#switchTpl', title: '订单状态'},
                    {templet: '#order-state', width: 120, align: 'center', title: '操作'}
                ]]
            });
        }
        order();
        // 工具条点击事件
        table.on('tool(menus-table)', function (obj) {
            var data = obj.data;
            var layEvent = obj.event;

            if (layEvent === 'edit') { // 修改
                showEditModel(data);
            } else if (layEvent === 'del') {
                layer.confirm('确定删除此菜单吗？', function () {
                    layer.load(2);

                    admin.req('api-administrators/order/delete/'+obj.data.ordeid, {access_token: config.getToken().access_token}, function (data) {
                        layer.closeAll('loading');
                        if (0 == data.code) {
                            layer.msg(data.msg, {icon: 1, time: 500});
                            order();
                        } else {
                            layer.msg(data.msg, {icon: 2, time: 500});
                        }
                    }, 'post');

                });
            }
        });

        // 显示编辑弹窗
        var showEditModel = function (data) {
            // debugger;
            // if (data) {
            //     data.isMenu = data.isMenu.toString();
            //     data.hidden = data.hidden.toString();
            // }
            var title = data ? '修改菜单' : '添加菜单';
            admin.putTempData('t_menus', data);

            admin.popupCenter({
                title: title,
                path: 'pages/system/order_form.html',
                finish: function () {
                    order();
                }
            });

        };

        //监听状态操作
        form.on('switch(order)', function (obj) {
                $.ajax({
                    url: config.base_server + 'api-administrators/order/updates',//地址：访问后台的地址
                    data: {ordeid: obj.value, access_token: config.getToken().access_token,},//前端传递给后台的数据
                    type: 'post',
                    dataType: 'json',//是后端传递到前端的数据格式
                    success: function (data) {//后端给前端的一个反馈
                        layer.msg('支付成功！')
                    }
                });
        });
        // 添加按钮点击事件
        $('#menus-btn-add').click(function () {
            showEditModel();
        });

        // 搜索按钮点击事件
        $('#menus-btn-search').click(function () {
            var keyword = $('#menus-edit-search').val();
            table.reload('order-table', {where: {orcode: keyword}});
        });
    });
</script>